# Given a set of words, group them into sets of anagrams.

words = ["delay","layde","tomato","matoto","nature","turena","inst"]

class String
  def to_anagram_key
    char_counting_table = Hash.new(0)
    self.each_char{|c| char_counting_table[c] += 1}
    ('a'..'z').inject([]) { |r,c| r << "#{c}#{char_counting_table[c]}" if char_counting_table[c] > 0;r}.join
  end
end

class Array
  def get_anagram_set
    self.inject(Hash.new([])) { |result,word| result[word.to_anagram_key] += [word]; result}.values
  end
end
p words.get_anagram_set

# Given a string, remove all the duplicate characters.


require 'set'

class String
  def remove_dup
    char_set = Set.new
    chars = []
    self.each_char do |c|      
      unless char_set.include? c
        chars << c
        char_set << c
      end      
    end
    chars.join
  end
end

p "abcedbe".remove_dup
